package card_game;

import java.util.List;
import java.util.ArrayList;

public class BlackJackHand extends Hand<BlackJackCard> {
    public BlackJackHand() {}

    private final int TWENTY_ONE = 21;
    public int score() {
	List<Integer> scores = possibleScores();
	int maxUnder = Integer.MIN_VALUE;
	int minOver = Integer.MAX_VALUE;
	for (int score : scores) {
	    if (score > TWENTY_ONE && score < minOver) {
		minOver = score;
	    } else if (score <= TWENTY_ONE && score > maxUnder) {
		maxUnder = score;
	    }
	}
	return maxUnder == Integer.MIN_VALUE ? minOver : maxUnder;
    }

    private List<Integer> possibleScores() {
	List<Integer> scores = new ArrayList<Integer>();
	if (cards.size() == 0)
	    return scores;
	for (BlackJackCard card : cards) {
	    addCardToScoreList(card, scores);
	}
	return scores;
    }

    private void addCardToScoreList(BlackJackCard card, List<Integer> scores) {
	if (scores.size() == 0) {
	    scores.add(0);
	}
	int length = scores.size();
	for (int i = 0; i < length; i++) {
	    int score = scores.get(i);
	    scores.set(i, score + card.minValue());
	    if (card.minValue() != card.maxValue()) {
		scores.add(score + card.maxValue());
	    }
	}
    }

    public boolean busted() {
	return score() > 21;
    }

    public boolean isTwentyOne() {
	return score() == 21;
    }
    

    public boolean isBlackJack() {
	if (cards.size() != 2) {
	    return false;
	}
	BlackJackCard first = cards.get(0);
	BlackJackCard second = cards.get(1);
	return (first.isAce() && second.isFaceCard()) || (second.isAce() && first.isAce());
    }
}